package com.iemes.controller.qc;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CorrelationDetection;
import com.iemes.entity.InventoryFormMap;
import com.iemes.entity.MeasurementTestForMap;
import com.iemes.entity.RelevanceForMap;
import com.iemes.mapper.MaterialQualityMapper;
import com.iemes.mapper.MeasurementTestMapper;
import com.iemes.mapper.ProjuctJcMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.List;

//成品计数质检
@Controller
@RequestMapping("/mt/")
public class MeasurementTestController extends BaseController {
    @Inject
    private MeasurementTestMapper measurementTestMapper;

    private String orderCode;


    @Inject
    private MaterialQualityMapper materialQualityMapper;

    @Inject
    private ProjuctJcMapper projuctJcMapper;

    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/qc/mt/mt_list";
    }

    //获取分页数据
    @ResponseBody
    @RequestMapping("getMtByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort) throws Exception {
        String myValue = getPara("myValue");
        if(myValue == null || myValue.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            PageInfo pageInfo = new PageInfo<>( measurementTestMapper.getAll());
            return pageInfo;

        }else {
            String[] split = myValue.split(",");
            if(split.length > 1){
                if(split[0].equals("name")){
                    //根据名称进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(measurementTestMapper.getData(split[1],"name"));
                    return pageInfo;
                }else {
                    //根据编号进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(measurementTestMapper.getData(split[1],"code"));
                    return pageInfo;
                }
            }else {
                PageInfo pageInfo = new PageInfo<>( measurementTestMapper.getAll());
                return pageInfo;
            }
        }
    }

    //打开添加页面
    @RequestMapping("openAdd")
    public String openProcessManagementUI(){
        return Common.BACKGROUND_PATH + "/qc/mt/mt_add";
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addMt")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addOrder(){
        MeasurementTestForMap measurementTestForMap = getFormMap(MeasurementTestForMap.class);
        try {
            measurementTestMapper.addEntity(measurementTestForMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("openEdit")
    public String openEditUi(Model model){
        String id = getPara("id");
        MeasurementTestForMap measurementTestForMap = measurementTestMapper.findbyFrist("id", id, MeasurementTestForMap.class);
        orderCode = measurementTestForMap.get("order_code").toString();

        List<CorrelationDetection> detection = materialQualityMapper.getCd(measurementTestForMap.get("product_count_code").toString());

        List<CorrelationDetection> materialId = materialQualityMapper.getMaterialId(detection.get(0).get("detection_code").toString());

        model.addAttribute("cd",materialId);

        model.addAttribute("mt",measurementTestForMap);
        return Common.BACKGROUND_PATH + "/qc/mt/mt_edit";
    }


    //保存修改数据
    @ResponseBody
    @RequestMapping("editMt")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editUn(String txtGroupsSelect) throws Exception {
        MeasurementTestForMap unqualifiedFormMap = getFormMap(MeasurementTestForMap.class);
        measurementTestMapper.deleteByAttribute("id", unqualifiedFormMap.get("id")+"", MeasurementTestForMap.class);
        unqualifiedFormMap.put("order_code",orderCode);
        measurementTestMapper.addEntity(unqualifiedFormMap);
        return "success";
    }

    //删除数据
    @ResponseBody
    @RequestMapping("delMt")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息删除")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delCheck() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            baseMapper.deleteByAttribute("id", id, MeasurementTestForMap.class);
        }
        return "success";
    }

    //获取数据
    @ResponseBody
    @RequestMapping("getRelevanceData")
    public String getRelevanceData(){
        String id = getPara("id");
        //根据物料名称获取信息mynumber
        InventoryFormMap inventory = measurementTestMapper.getInventory(id);
        List<RelevanceForMap> id1 = projuctJcMapper.selRelevanceName(inventory.get("id").toString());
        for (RelevanceForMap bc: id1) {
            bc.put("mynumber",inventory.get("id").toString());
        }
        return JSON.toJSONString(id1);
    }
}
